package com.example.study.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.study.domain.*;
import com.example.study.service.IClasszService;
import com.example.study.service.ICourseService;
import com.example.study.service.ISelectCourseService;
import com.example.study.service.ITeacherService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequiredArgsConstructor
public class IndexController {

    private final IClasszService classzService;

    private final ISelectCourseService selectCourseService;

    private final ICourseService courseService;

    private final ITeacherService teacherService;

    @GetMapping("/index")
    public String indexPage(HttpSession httpSession) {
        StudentDO student = (StudentDO) httpSession.getAttribute("student");
        TeacherDO teacherDO = (TeacherDO) httpSession.getAttribute("teacher");
        if (BeanUtil.isNotEmpty(student)) {
            // 设置班级信息
            ClasszDO byIdClasszDO = classzService.getById(student.getClassId());
            // 设置课程信息
            List<Long> selectedId = selectCourseService.list(new LambdaQueryWrapper<SelectCourseDO>()
                            .eq(SelectCourseDO::getStudentId, student.getId()))
                    .stream().map(item -> item.getCourseId()).collect(Collectors.toList());
            if (selectedId.size() > 0) {
                List<CourseDO> selectedCourse = courseService.listByIds(selectedId);
                selectedCourse = selectedCourse.stream().map(item -> {
                    item.setClasszName(byIdClasszDO.getName());
                    TeacherDO byIdTeacherDO = teacherService.getById(item.getTeacherId());
                    item.setTeacherName(byIdTeacherDO.getName());
                    SelectCourseDO oneSelectCourseDO = selectCourseService.getOne(new LambdaQueryWrapper<SelectCourseDO>()
                            .eq(SelectCourseDO::getStudentId, student.getId())
                            .eq(SelectCourseDO::getCourseId, item.getId()));
                    item.setScore(oneSelectCourseDO.getScore());
                    return item;
                }).collect(Collectors.toList());
                httpSession.setAttribute("selectedCourse",selectedCourse);
            }
        }
        if (BeanUtil.isNotEmpty(teacherDO)) {
            List<CourseDO> list = courseService.list(new LambdaQueryWrapper<CourseDO>()
                    .eq(CourseDO::getTeacherId, teacherDO.getId()));
            if (CollUtil.isNotEmpty(list)) {
                list = list.stream().map(item -> {
                    item.setTeacherName(teacherService.getById(item.getTeacherId()).getName());
                    item.setClasszName(classzService.getById(item.getClasszId()).getName());
                    return item;
                }).collect(Collectors.toList());
            }
            httpSession.setAttribute("teacherCourse",list);
        }
        return "index";
    }
}
